﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace WooCoo.ObjectModel.ManagedProperty.PropertyDescriptors
{
    /// <summary>
    /// 托管属性的描述器
    /// </summary>
    public class ManagedPropertyDescriptor : PropertyDescriptor
    {
        private IPropertyInfo _property;

        internal ManagedPropertyDescriptor(IPropertyInfo property)
            : base(property.Name, null)
        {
            this._property = property;
        }

        public override void AddValueChanged(object component, EventHandler handler)
        {
            throw new NotSupportedException();
        }

        public override Type ComponentType
        {
            get { return typeof(ManagedPropertyBase); }
        }

        public override bool IsReadOnly
        {
            get { return this._property.isReadOnly; }
        }

        public override Type PropertyType
        {
            get { return this._property.Type; }
        }

        public override bool ShouldSerializeValue(object component)
        {
            return false;
        }

        public override bool CanResetValue(object component)
        {
            return true;
        }

        public override object GetValue(object component)
        {
            return (component as ManagedPropertyBase).GetProperty(this._property);
        }

        public override void ResetValue(object component)
        {
            (component as ManagedPropertyBase).ResetProperty(this._property);
        }

        public override void SetValue(object component, object value)
        {
            //使用 ManagedPropertyDescriptor 来进行操作属性时，默认都是作为 UI 层操作。
            (component as ManagedPropertyBase).SetProperty(this._property, value);
        }        
    }
}
